<?php
include_once $REP_CLASS . 'Selector.php';
include_once $REP_CLASS . 'Utilevt.php';
//include_once $REP_CLASS . 'Lstthemeevent.php';

class Evenement {

private $refevenement  = 0;
private $libelle;
private $datedeb;
private $datefin;
private $frequence;
private $jour;
private $heuredeb;
private $heurefin;
private $lieu;
private $adressel1;
private $adressel2;
private $insee;
private $inscription;
private $tarif1;
private $tarif2;
private $url;
private $commentaire;
private $refassociation;
private $dateinscription;
private $assocPDO;
private $sqlInsert;
private $reqInsert;
private $sqlDelete;
private $reqDelete;
private $sqlUpdate;
private $reqUpdate;
private $sqlSelectByAssoc;
private $reqSelectByAssoc;
private $refutilisateur;
private $reftheme;
private $refgroupetheme;
private $dept;


/**
 * Constructeur
 * @db : la connexion de la base de donn?e
 */
 function __construct($db){

      // Cr?ation de la connexion
      $this->assocPDO = $db;
      // Initialisation des requ?tes
      $this->initRequete();
      // Pr?paration des requ?tes
      $this->prepare();

}
/**
 * Initialisation des requ?tes pr?par?es
 */
private function initRequete(){

      $this->sqlInsert = "INSERT INTO evenement (libelle, datedeb, datefin, frequence, jour, heuredeb, heurefin, lieu, adressel1, adressel2, insee, inscription, tarif1, tarif2, url, commentaire, refassociation, dateinscription, refgroupetheme, reftheme ) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?)";
      $this->sqlDelete = "DELETE FROM evenement WHERE `refevenement` = ?";
      $this->sqlUpdate = "UPDATE evenement SET refevenement=?, libelle=?, datedeb=?, datefin=?, frequence=?, jour=?, heuredeb=?, heurefin=?, lieu=?, adressel1=?, adressel2=?, insee=?, inscription=?, tarif1=?, tarif2=?, url=?, commentaire=?, refassociation=?, dateinscription=?, refgroupetheme=?, reftheme=? WHERE `refevenement` = ?";
      $this->sqlSelectByAssoc = "SELECT e.refevenement, e.libelle as libevent, e.datedeb, a.libelle as libassoc, c.libelle as libcommune, c.dept
                   FROM evenement e left join association a on (e.refassociation=a.refassociation) left join commune c
                   on (c.insee=e.insee) order by e.dateinscription desc";

}
/**
 * Pr?paration des requ?tes
 */
private function prepare(){

      $this->reqInsert = $this->assocPDO->prepare($this->sqlInsert);
      $this->reqDelete = $this->assocPDO->prepare($this->sqlDelete);
      $this->reqUpdate = $this->assocPDO->prepare($this->sqlUpdate);
   	  $this->reqSelectByAssoc = $this->assocPDO->prepare($this->sqlSelectByAssoc);
}
/**
 * Insertion en base des infos de l'utilisateur
 */
public function insertion(){

      // Requete d'insertion
      $champs = array($this->libelle, $this->datedeb, $this->datefin, $this->frequence, $this->jour, $this->heuredeb, $this->heurefin, $this->lieu, $this->adressel1, $this->adressel2, $this->insee, $this->inscription, $this->tarif1, $this->tarif2, $this->url, $this->commentaire, $this->refassociation, $this->dateinscription, $this->refgroupetheme, $this->reftheme);

      try
      {
         $this->reqInsert->execute($champs);


		 $stmt = $this->select();
      	 $result = $stmt->fetch();
	     $this->setRefevenement($result['refevenement']);

         // si le refutilisateur est renseigne, on insere le lien
         if ($this->getRefutilisateur()!="")
         {

         	$utilevt = new Utilevt($this->assocPDO);
         	$utilevt->setRefutilisateur($this->getRefutilisateur());
         	$utilevt->setRefevenement($this->getRefevenement());
         	$utilevt->insertion();
         }

         return $this->getRefevenement();
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'; print_r($this->reqInsert->errorInfo());
         throw new Exception('Une erreur est survenue lors de l\'insertion dans evenement.' . $e->getMessage());
      }

}
/**
 * Modification des infos de l'utilisateur
 */
public function modification(){

      // Requete de modification
      $champs = array($this->refevenement, $this->libelle, $this->datedeb, $this->datefin, $this->frequence, $this->jour, $this->heuredeb, $this->heurefin, $this->lieu, $this->adressel1, $this->adressel2, $this->insee, $this->inscription, $this->tarif1, $this->tarif2, $this->url, $this->commentaire, $this->refassociation, $this->dateinscription, $this->refgroupetheme, $this->reftheme);

      try
      {
         return $this->reqUpdate->execute($champs);
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'; print_r($this->reqUpdate->errorInfo());
         throw new Exception('Une erreur est survenue lors de la modification dans evenement.');
      }

}
/**
 * Suppression de l'utilisateur ? partir de son ID
 */
public function suppression(){

      // Requete de suppression
      try
      {
         return $this->reqDelete->execute(array($this->refevenement));
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'; print_r($this->reqDelete->errorInfo());
         throw new Exception('Une erreur est survenue lors de la suppression dans evenement.');
      }

}
/**
 * Select
 */
public function select(){

      $select = new Selector();
      $champs = array("refevenement"=>$this->refevenement, "libelle"=>$this->libelle, "datedeb"=>$this->datedeb, "datefin"=>$this->datefin, "frequence"=>$this->frequence, "jour"=>$this->jour, "heuredeb"=>$this->heuredeb, "heurefin"=>$this->heurefin, "lieu"=>$this->lieu, "adressel1"=>$this->adressel1, "adressel2"=>$this->adressel2, "insee"=>$this->insee, "inscription"=>$this->inscription, "tarif1"=>$this->tarif1, "tarif2"=>$this->tarif2, "url"=>$this->url, "commentaire"=>$this->commentaire, "refassociation"=>$this->refassociation, "dateinscription"=>$this->dateinscription, "refgroupetheme"=>$this->refgroupetheme, "reftheme"=>$this->reftheme);

      foreach($champs as $cle=>$valeur)
      {
        // Les Champs
        $select->addChamp($cle);
        // Les Crit?res
        if( !empty($valeur) )
         $select->addCritere($cle, $valeur);
      }

      // La Table
      $select->addFrom('evenement');

      //echo 'requete : ' . $select->toString();
      return $this->assocPDO->query($select->toString());

}
/**
 * Charge les infos de evenement
 */
public function charge(){

      $user = new Evenement($this->assocPDO);
      $user->setRefevenement($this->getRefevenement());
      // Lecture de la premi?re ligne
      $stmt = $user->select();
      $result = $stmt->fetch();

      $this->refevenement=$result['refevenement'];
      $this->libelle=$result['libelle'];
      $this->datedeb=$result['datedeb'];
      $this->datefin=$result['datefin'];
      $this->frequence=$result['frequence'];
      $this->jour=$result['jour'];
      $this->heuredeb=$result['heuredeb'];
      $this->heurefin=$result['heurefin'];
      $this->lieu=$result['lieu'];
      $this->adressel1=$result['adressel1'];
      $this->adressel2=$result['adressel2'];
      $this->insee=$result['insee'];
      $this->inscription=$result['inscription'];
      $this->tarif1=$result['tarif1'];
      $this->tarif2=$result['tarif2'];
      $this->url=$result['url'];
      $this->commentaire=$result['commentaire'];
      $this->refassociation=$result['refassociation'];
      $this->dateinscription=$result['dateinscription'];
      $this->refgroupetheme=$result['refgroupetheme'];
      $this->reftheme=$result['reftheme'];

      // Fermeture du curseur
      $stmt->closeCursor();

}

public function selectByAssoc()
{
 	  // Requete de suppression
      try
      {
         $this->reqSelectByAssoc->execute();
         return $this->reqSelectByAssoc->fetchAll();
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'; print_r($this->reqSelectByAssoc->errorInfo());
         throw new Exception('Une erreur est survenue lors du select par association dans evenement.');
      }
}

/**
 * Affiche les infos de l'utilisateur
 */
public function affichage(){

      $champs = array("refevenement"=>$this->refevenement, "libelle"=>$this->libelle, "datedeb"=>$this->datedeb, "datefin"=>$this->datefin, "frequence"=>$this->frequence, "jour"=>$this->jour, "heuredeb"=>$this->heuredeb, "heurefin"=>$this->heurefin, "lieu"=>$this->lieu, "adressel1"=>$this->adressel1, "adressel2"=>$this->adressel2, "insee"=>$this->insee, "inscription"=>$this->inscription, "tarif1"=>$this->tarif1, "tarif2"=>$this->tarif2, "url"=>$this->url, "commentaire"=>$this->commentaire, "refassociation"=>$this->refassociation, "dateinscription"=>$this->dateinscription, "refgroupetheme"=>$this->refgroupetheme, "reftheme"=>$this->reftheme);
      echo "==== Fiche Evenement ==== <br>";
      foreach($champs as $cle=>$valeur)
      {
        echo $cle . " : " . $valeur;
        echo "<br>";
      }
      echo "==========================<br>";

}






/**
 * Select des events selon les criteres saisis
 */
public function selectEvent(){
      try
      {
      	 $req= "SELECT evenement.refevenement as refevenement, evenement.libelle as libelle,DATE_FORMAT(datedeb,'%d/%m/%Y') as datedeb ,
      	 		DATE_FORMAT(datefin,'%d/%m/%Y') as datefin,frequence, jour, heuredeb, heurefin, lieu, evenement.adressel1 as adressel1,
      	 		evenement.adressel2 as adressel2, evenement.insee as insee, inscription, tarif1, tarif2, url, commentaire, evenement.refassociation as refassociation,
      	 		evenement.refgroupetheme as refgroupetheme,evenement.reftheme as theme,
      	 		dept.dept as dept,commune.libelle libcommune,commune.cp cp, dept.libelle as libdept,
      	 		groupetheme.libelle as libgroupetheme, theme.libelle as libtheme, association.libelle as libassoc,
      	 		association.siteweb as siteweb
               FROM evenement left join commune on (commune.insee=evenement.insee)
               left join groupetheme on (groupetheme.refgroupetheme=evenement.refgroupetheme)
               left join theme on (theme.refgroupetheme=evenement.refgroupetheme and theme.reftheme=evenement.reftheme)
               left join dept on (dept.dept=commune.dept)
               left join association on (evenement.refassociation=association.refassociation) where 1=1";

         if ($this->getRefevenement()!='')
         	$req=$req . " and refevenement='".$this->getRefevenement()."'";

         if ($this->getRefgroupetheme()!='')
         	$req=$req . " and evenement.refgroupetheme='".$this->getRefgroupetheme()."'";

         if ($this->getReftheme()!='')
         	$req=$req . " and evenement.reftheme='".$this->getReftheme()."'";

         if ($this->getDatedeb()!='')
         	$req=$req . " and str_to_date('".$this->getDatedeb()."','%d/%m/%Y') between ifnull(datedeb,str_to_date('01/01/2000','%d/%m/%Y')) and ifnull(datefin,str_to_date('01/01/2100','%d/%m/%Y'))";

         if ($this->getInsee()!='')
         	$req=$req . " and evenement.insee='".$this->getInsee()."'";

         if ($this->getDept()!='')
         	$req=$req . " and dept.dept='".$this->getDept()."'";

         if ($this->getCommentaire()!='')
         {	$req=$req . " and (commentaire like '%".str_replace("'","''",$this->getCommentaire())."%' or evenement.libelle like '%".str_replace("'","''",$this->getCommentaire())."%')";

         }
		 $req=$req . " order by evenement.datedeb desc ";

      	 return $this->assocPDO->query($req);
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'.$req;
         throw new Exception('Une erreur est survenue lors de la recherche des annonces.');
      }
}




/**
 * Retrieves the value from the field refevenement
 * @return String with the value of the field
 */
public function getRefevenement(){
  return $this->refevenement;
}
/**
 * Retrieves the value from the field libelle
 * @return String with the value of the field
 */
public function getLibelle(){
  return $this->libelle;
}
/**
 * Retrieves the value from the field datedeb
 * @return String with the value of the field
 */
public function getDatedeb(){
  return $this->datedeb;
}
/**
 * Retrieves the value from the field datefin
 * @return String with the value of the field
 */
public function getDatefin(){
  return $this->datefin;
}
/**
 * Retrieves the value from the field frequence
 * @return String with the value of the field
 */
public function getFrequence(){
  return $this->frequence;
}
/**
 * Retrieves the value from the field jour
 * @return String with the value of the field
 */
public function getJour(){
  return $this->jour;
}
/**
 * Retrieves the value from the field heuredeb
 * @return String with the value of the field
 */
public function getHeuredeb(){
  return $this->heuredeb;
}
/**
 * Retrieves the value from the field heurefin
 * @return String with the value of the field
 */
public function getHeurefin(){
  return $this->heurefin;
}
/**
 * Retrieves the value from the field lieu
 * @return String with the value of the field
 */
public function getLieu(){
  return $this->lieu;
}
/**
 * Retrieves the value from the field adressel1
 * @return String with the value of the field
 */
public function getAdressel1(){
  return $this->adressel1;
}
/**
 * Retrieves the value from the field adressel2
 * @return String with the value of the field
 */
public function getAdressel2(){
  return $this->adressel2;
}
/**
 * Retrieves the value from the field insee
 * @return String with the value of the field
 */
public function getInsee(){
  return $this->insee;
}
/**
 * Retrieves the value from the field inscription
 * @return String with the value of the field
 */
public function getInscription(){
  return $this->inscription;
}
/**
 * Retrieves the value from the field tarif1
 * @return String with the value of the field
 */
public function getTarif1(){
  return $this->tarif1;
}
/**
 * Retrieves the value from the field tarif2
 * @return String with the value of the field
 */
public function getTarif2(){
  return $this->tarif2;
}
/**
 * Retrieves the value from the field url
 * @return String with the value of the field
 */
public function getUrl(){
  return $this->url;
}
/**
 * Retrieves the value from the field commentaire
 * @return String with the value of the field
 */
public function getCommentaire(){
  return $this->commentaire;
}
/**
 * Retrieves the value from the field refassociation
 * @return String with the value of the field
 */
public function getRefassociation(){
  return $this->refassociation;
}
/**
 * Retrieves the value from the field Dateinscription
 * @return String with the value of the field
 */
public function getDateinscription(){
  return $this->dateinscription;
}

public function getReftheme(){
  return $this->reftheme;
}

public function getRefgroupetheme(){
  return $this->refgroupetheme;
}


/**************** NON BASE *********************/

public function getRefutilisateur(){
  return $this->refutilisateur;
}

public function getDept(){
  return $this->dept;
}

/**
 * Set the value from the field refevenement
 * @param refevenement String with the value for the field
 */
public function setRefevenement($refevenement){
  $this->refevenement = $refevenement;
}
/**
 * Set the value from the field libelle
 * @param libelle String with the value for the field
 */
public function setLibelle($libelle){
  $this->libelle = $libelle;
}
/**
 * Set the value from the field datedeb
 * @param datedeb String with the value for the field
 */
public function setDatedeb($datedeb){
  $this->datedeb = $datedeb;
}
/**
 * Set the value from the field datefin
 * @param datefin String with the value for the field
 */
public function setDatefin($datefin){
  $this->datefin = $datefin;
}
/**
 * Set the value from the field frequence
 * @param frequence String with the value for the field
 */
public function setFrequence($frequence){
  $this->frequence = $frequence;
}
/**
 * Set the value from the field jour
 * @param jour String with the value for the field
 */
public function setJour($jour){
  $this->jour = $jour;
}
/**
 * Set the value from the field heuredeb
 * @param heuredeb String with the value for the field
 */
public function setHeuredeb($heuredeb){
  $this->heuredeb = $heuredeb;
}
/**
 * Set the value from the field heurefin
 * @param heurefin String with the value for the field
 */
public function setHeurefin($heurefin){
  $this->heurefin = $heurefin;
}
/**
 * Set the value from the field lieu
 * @param lieu String with the value for the field
 */
public function setLieu($lieu){
  $this->lieu = $lieu;
}
/**
 * Set the value from the field adressel1
 * @param adressel1 String with the value for the field
 */
public function setAdressel1($adressel1){
  $this->adressel1 = $adressel1;
}
/**
 * Set the value from the field adressel2
 * @param adressel2 String with the value for the field
 */
public function setAdressel2($adressel2){
  $this->adressel2 = $adressel2;
}
/**
 * Set the value from the field insee
 * @param insee String with the value for the field
 */
public function setInsee($insee){
  $this->insee = $insee;
}
/**
 * Set the value from the field inscription
 * @param inscription String with the value for the field
 */
public function setInscription($inscription){
  $this->inscription = $inscription;
}
/**
 * Set the value from the field tarif1
 * @param tarif1 String with the value for the field
 */
public function setTarif1($tarif1){
  $this->tarif1 = $tarif1;
}
/**
 * Set the value from the field tarif2
 * @param tarif2 String with the value for the field
 */
public function setTarif2($tarif2){
  $this->tarif2 = $tarif2;
}
/**
 * Set the value from the field url
 * @param url String with the value for the field
 */
public function setUrl($url){
  $this->url = $url;
}
/**
 * Set the value from the field commentaire
 * @param commentaire String with the value for the field
 */
public function setCommentaire($commentaire){
  $this->commentaire = $commentaire;
}
/**
 * Set the value from the field refassociation
 * @param refassociation String with the value for the field
 */
public function setRefassociation($refassociation){
  $this->refassociation = $refassociation;
}

public function setDateinscription($dateinscription){
  $this->dateinscription = $dateinscription;
}


public function setReftheme($reftheme){
  $this->reftheme = $reftheme;
}

public function setRefgroupetheme($refgroupetheme){
  $this->refgroupetheme = $refgroupetheme;
}

/************************** NON BASE ********************/
public function setRefutilisateur($refutilisateur){
  $this->refutilisateur = $refutilisateur;
}

public function setDept($dept){
  $this->dept = $dept;
}


}
?>